package com.glls.order.controller;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.glls.common.R;
import com.glls.common.vo.OrderVo;
import com.glls.order.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author junyang
 * @version 1.0.0
 * @date 2025/2/24 10:40
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;


    @RequestMapping("/tran1")
    public R noTransaction(@RequestBody OrderVo order){

        int result = orderService.addOrderWithNoTransaction(order);
        if(result == 1){
            return R.ok().message("添加订单成功");
        }
        return R.error().message("添加订单失败");
    }



    @RequestMapping("/tran2")
    public R localTransaction(@RequestBody OrderVo order) throws ClassNotFoundException {

        int result = orderService.addOrderWithLocalTransaction(order);
        if(result == 1){
            return R.ok().message("添加订单成功");
        }
        return R.error().message("添加订单失败");
    }


    //预留作业   演示分布式事务场景
    @RequestMapping("/tran3")
    public R distributeTransaction(@RequestBody OrderVo order) {

        int result = orderService.addOrderWithDistributeTransaction(order);
        if(result == 1){
            return R.ok().message("添加订单成功");
        }
        return R.error().message("添加订单失败");
    }


    @RequestMapping("/test1")
    public String testSentinel1(){

        log.info("order-test1");

        return "service-order-test1";
    }

    @RequestMapping("/test2")
    public String testSentinel2(){
        // 演示关联
        log.info("order-test2");

        return "service-order-test2";
    }



    @RequestMapping("/test3")
    public String test3(String name){

        log.info("order-test3");

        // 演示 流控模式  链路
        String result = orderService.test3(name);

        return "service-order-test3:"+result;
    }

    @RequestMapping("/test4")
    public String test4(String name){

        String result = orderService.test3(name);
        return "service-order-test4:"+result;
    }

    @RequestMapping("/test5")
    public String test5(String name) throws InterruptedException {
        //演示 熔断
        log.info("order-test5");

        if(name.equals("error")){
            TimeUnit.SECONDS.sleep(2);
        }

        return "service-order-test5:";
    }

    @RequestMapping("/test6")
    public String test6(String name,Integer age) throws InterruptedException {
        // 注解的形式定义资源
        log.info("order-test6");

        String result = orderService.test6(name,age);

        return "service-order-test6:"+ result;
    }

    @RequestMapping("/test7")
    public String test7(String name) throws InterruptedException, BlockException {
        //演示 try catch 形式定义资源
        log.info("order-test6");

        String result = orderService.test7(name);

        return "service-order-test7:"+ result;
    }
}
